package com.wangpeng.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wangpeng.commons.result.Tree;
import com.wangpeng.mapper.NColumnMapper;
import com.wangpeng.model.NColumn;
import com.wangpeng.service.INColumnService;
import com.baomidou.framework.service.impl.SuperServiceImpl;

/**
 *
 * NColumn 表数据服务层接口实现类
 *
 */
@Service
public class NColumnServiceImpl extends SuperServiceImpl<NColumnMapper, NColumn> implements INColumnService {
		
		@Autowired
	    private NColumnMapper columnMapper;
	    
	    @Override
	    public List<Tree> selectTree() {
	        List<Tree> trees = new ArrayList<Tree>();
	        //查询一级菜单
	        List<NColumn> columnFather = columnMapper.selectByPIdNull();

	        if (columnFather != null) {
	            for (NColumn columnOne : columnFather) {
	                Tree treeOne = new Tree();

	                treeOne.setId(columnOne.getId());
	                treeOne.setText(columnOne.getName());
	                treeOne.setIconCls(columnOne.getIcon());
	                //查询子菜单
	                List<NColumn> columnSon = columnMapper.selectAllByPId(columnOne.getId());

	                if (columnSon != null) {
	                    List<Tree> tree = new ArrayList<Tree>();
	                    for (NColumn columnTwo : columnSon) {
	                        Tree treeTwo = new Tree();
	                        treeTwo.setId(columnTwo.getId());
	                        treeTwo.setText(columnTwo.getName());
	                        treeTwo.setIconCls(columnTwo.getIcon());
	                        tree.add(treeTwo);
	                    }
	                    treeOne.setChildren(tree);
	                } else {
	                    treeOne.setState("closed");
	                }
	                trees.add(treeOne);
	            }
	        }
	        return trees;
	    }

	    @Override
	    public List<NColumn> selectTreeGrid() {
	        return columnMapper.selectAll();
	    }

}